package com.building.hcrm.ui;

import com.building.hcrm.R;
import com.building.hcrm.R.layout;
import com.building.hcrm.base.BaseActivity;
import com.building.hcrm.common.ComConst;
import com.building.hcrm.common.UpdateManager;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;

public class LoadingActivity extends BaseActivity
{
	private Handler mHandler;
	private boolean mRunning = true;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_loading);
		
		HandlerThread thread = new HandlerThread("WaitHandlerThread");
		thread.start();// 创建一个HandlerThread并启动它
		mHandler = new Handler(thread.getLooper());// 使用HandlerThread的looper对象创建Handler，如果使用默认的构造方法，很有可能阻塞UI线程
		mHandler.post(mBackgroundRunnable);// 将线程post到Handler中
	}
	
	Runnable mBackgroundRunnable = new Runnable() {
		@Override
		public void run() {

			while (mRunning) {
				try
				{
					Thread.sleep(2000);

					UpdateManager upManager = new UpdateManager(LoadingActivity.this, handler);
					upManager.checkUpdate();
				}
				catch (Exception e)
				{
					ShowNoNetwork();
				}
				finally
				{
					mRunning=false;
				}
			}
		}
	};

	private void ShowNoNetwork()
	{
		if (!LoadingActivity.this.isFinishing())
		{
			AlertDialog.Builder builder = new Builder(LoadingActivity.this);
			builder.setMessage(R.string.no_network_msg);
			builder.setPositiveButton(R.string.cancel, new OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					dialog.dismiss();
					LoadingActivity.this.finish();
				}
			});

			builder.setNegativeButton(R.string.confirm, new OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					dialog.dismiss();
					Message msg = Message.obtain();
					msg.what = ComConst.RECONNECT;
					handler.sendMessage(msg);
				}
			});

			Dialog noticeDialog = builder.create();
			noticeDialog.setCancelable(false);// 禁止返回键关闭
			noticeDialog.show();
		}
	}
	
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
		
			switch (msg.what) {
				case ComConst.RECONNECT:
					mRunning = true;
					mBackgroundRunnable.run();
					break;
				case ComConst.UPDATE_FINISH :
					Intent intent=new Intent();
					intent.setClass(LoadingActivity.this, MainActivity.class);
					startActivity(intent);
					break;
				default :
					break;
			}
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 销毁线程
		mHandler.removeCallbacks(mBackgroundRunnable);
	}
}
